package com.example.pingmiantutui.ui.mistake_collection

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.pingmiantutui.R
import com.example.pingmiantutui.data.MistakeQuestion

class MistakeQuestionPagerAdapter : BaseMistakeQuestionAdapter() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        try {
            return when (viewType) {
                VIEW_TYPE_SINGLE -> SingleQuestionViewHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_single_question, parent, false)
                )
                VIEW_TYPE_CONTRAST -> ContrastQuestionViewHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_contrast_question, parent, false)
                )
                VIEW_TYPE_ANALYSE -> AnalyseQuestionViewHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_analyse_question, parent, false)
                )
                else -> throw IllegalArgumentException("Unknown view type: $viewType")
            }
        } catch (e: Exception) {
            Toast.makeText(parent.context, "创建ViewHolder失败: ${e.message}", Toast.LENGTH_LONG).show()
            throw e
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        try {
            val question = getItem(position)
            when (holder) {
                is SingleQuestionViewHolder -> holder.bind(question)
                is ContrastQuestionViewHolder -> holder.bind(question)
                is AnalyseQuestionViewHolder -> holder.bind(question)
            }
        } catch (e: Exception) {
            Toast.makeText(holder.itemView.context, "绑定数据失败: ${e.message}", Toast.LENGTH_LONG).show()
            e.printStackTrace()
        }
    }
}